package com.example.zzl.viewmodel.HomeFragment;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.zzl.R;
import com.example.zzl.adapter.BaseCarouselPagerAdapter;
import com.example.zzl.adapter.HomeFragment.InfoAdapter;
import com.example.zzl.adapter.HomeFragment.InfoFootAdapter;
import com.example.zzl.app.MyApplication;
import com.example.zzl.databinding.ActivityInfoBinding;
import com.example.zzl.databinding.InfoFootBinding;
import com.example.zzl.databinding.InfoHeadBinding;
import com.example.zzl.gen.CartDao;
import com.example.zzl.model.HomeFragment.InfoModel;
import com.example.zzl.model.bean.Cart;
import com.example.zzl.model.bean.Info;
import com.example.zzl.model.bean.InfoFoot;
import com.example.zzl.utils.GreenDaoManager;
import com.example.zzl.view.BaseView;
import com.example.zzl.view.activity.UserActivity.LoginActivity;
import com.example.zzl.viewmodel.BaseViewModel;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by ligg on 2017/2/14.
 */

public class InfoPresenter implements BaseViewModel {

    private final InfoModel model;
    public Context context;
    public InfoCallback callback;
    public ActivityInfoBinding db;
    private List<Object> list;
    private InfoAdapter adapter;
    private InfoHeadBinding headDb;
    private List<String> headList;
    private BaseCarouselPagerAdapter headAdapter;
    private List<InfoFoot> footList;
    private InfoFootAdapter footAdapter;
    private InfoFootBinding footDb;
    private CartDao cartDao;
    private Info info;

    public InfoPresenter(Context context, InfoCallback callback, ActivityInfoBinding db) {
        this.context = context;
        this.callback = callback;
        this.db = db;
        model = new InfoModel(context, this);
    }

    public interface InfoCallback {

    }
    @Override
    public void init() {
        list = new ArrayList<Object>();
        adapter = new InfoAdapter(context, list);

        headList = new ArrayList<String>();
        headAdapter = new BaseCarouselPagerAdapter(context, headList);

        footList = new ArrayList<InfoFoot>();
        footAdapter = new InfoFootAdapter(context, footList);
        cartDao = GreenDaoManager.getInstance(context).getNewSession().getCartDao();
    }

    @Override
    public void initView() {
        headDb = DataBindingUtil.bind(View.inflate(context, R.layout.info_head, null));
        headDb.vp.setAdapter(headAdapter);

        footDb = DataBindingUtil.bind(View.inflate(context, R.layout.info_foot, null));
        footDb.gv.setAdapter(footAdapter);
        footDb.gv.setFocusable(false);

        db.rv.setLayoutManager(new LinearLayoutManager(context));
        db.rv.addHeaderView(headDb.getRoot());
        db.rv.addFootView(footDb.getRoot());
        db.rv.setAdapter(adapter);
        db.tvAddCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(info != null) {
                    if (MyApplication.nichName != null) {
                        Cart cart = new Cart();
                        cart.setImgUrl(info.getGetInfoData().getResult().strMainImage);
                        cart.setTitle(info.getGetInfoData().getResult().strInfoTitle);
                        cart.setContent(info.getGetInfoData().getResult().strInfoContent);
                        cart.setPrice(info.getGetInfoData().getResult().decMinPrice);
                        cart.setNickName(MyApplication.nichName);
                        cart.setCount(1);
                        cart.setStatus(0);
                        cartDao.insert(cart);
                        Toast.makeText(context, "加入购物车成功", Toast.LENGTH_SHORT).show();
                    } else {
                        context.startActivity(new Intent(context, LoginActivity.class));
                    }
                }else{
                    Toast.makeText(context, "数据加载中.....", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    @Override
    public void loadFinished() {
        info = model.getInfo();
        List<InfoFoot> footList = model.getList();
        if (info != null) {
            if (headList.size() == 0) {
                headDb.setHeadResult(info.GetInfoData.result);
                String strImageArray = info.GetInfoData.result.strImageArray;
                if (strImageArray != null) {
                    String[] split = strImageArray.split(",");
                    headList.addAll(Arrays.asList(split));
                } else {
                    headList.add(info.GetInfoData.result.strMainImage);
                }
                headAdapter.notifyDataSetChanged();
                headDb.vp.setCurrentItem(200, false);

                adapter.initData(info);
                adapter.notifyDataSetChanged();
            }

            if (footList != null) {
                Log.e("InfoPresenter", "loadFinished: ");
                this.footList.addAll(footList);
                BaseView.setListViewHeightBasedOnChildren(footDb.gv);
                footAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void loadData(String parameter) {
        model.loadData(parameter);
    }

}
